package com.ruix.app.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.PointF;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.ImageViewState;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.ruix.app.R;
import com.ruix.app.RuiApplication;
import com.ruix.app.util.ViewUtil;
import com.ruix.app.widget.photoview.PhotoViewAttacher;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by hadexs on 2017/5/14.
 */

public class PhotoViewAdapter extends PagerAdapter implements PhotoViewAttacher.OnPhotoTapListener {


    private Context context;
    private ArrayList<String> picAddress;
    private LoadPhotoListener loadPhotoListener;
    private List<View> mViewList;
    private int windWidth;
    private int windHeight;

    public PhotoViewAdapter(Context context) {
        this.context = context;
    }

    public PhotoViewAdapter(Context context, ArrayList<String> picAddress) {
        this.context = context;
        this.picAddress = picAddress;
        windWidth = ViewUtil.getWindowWidth(context);
        windHeight = ViewUtil.getWindowHeight(context);

    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View itemView = LayoutInflater.from(context)
                .inflate(R.layout.layout_photo_view, container, false);
//        final ImageView smallPhoto = (ImageView) itemView.findViewById(R.id.iv_photo_small);
//        final PhotoView imageView = (PhotoView) itemView.findViewById(R.id.iv_photo_pic);
//        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
//        imageView.enable();

        final SubsamplingScaleImageView scaleImageView = (SubsamplingScaleImageView) itemView.findViewById(R.id.imageView);

        scaleImageView.setMaxScale(15.0f);
        scaleImageView.setZoomEnabled(true);
        scaleImageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CUSTOM);

        if (loadPhotoListener != null) {
            loadPhotoListener.startLoading();
        }
        Glide.with(RuiApplication.getAppContext())
                .load(picAddress.get(position))
                .downloadOnly(new SimpleTarget<File>() {
            @Override
            public void onResourceReady(File resource, GlideAnimation<? super File> glideAnimation) {

//                ImageSource imageSource = ImageSource.uri(Uri.fromFile(resource));
                int  sWidth = BitmapFactory.decodeFile(resource.getAbsolutePath()).getWidth();
                int sHeight = BitmapFactory.decodeFile(resource.getAbsolutePath()).getHeight();
//                float scale = SystemUtil.displaySize.x / (float) sWidth;
                //float centerX = SystemUtil.displaySize.x / 2;
                // imageView.setImage(ImageSource.uri(Uri.fromFile(resource)), new ImageViewState(2.0F, new PointF(0, 0), 0));
                //imageView.setImage(ImageSource.uri(Uri.fromFile(resource)), new ImageViewState(2.0F, new PointF(0, 0), 0));
                if (sHeight >= windHeight
                        && sHeight / sWidth >=3) {
                    scaleImageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_CROP);
                    scaleImageView.setImage(ImageSource.uri(Uri.fromFile(resource)), new ImageViewState(2.0F, new PointF(0, 0), 0));
                }else {
                    scaleImageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CUSTOM);
                    scaleImageView.setImage(ImageSource.uri(Uri.fromFile(resource)));
                    scaleImageView.setDoubleTapZoomStyle(SubsamplingScaleImageView.ZOOM_FOCUS_CENTER_IMMEDIATE);
                }

                if (loadPhotoListener != null) {
                    loadPhotoListener.loadingFinish();
                }
            }
        });
        scaleImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (context instanceof Activity) {
                    if (!((Activity) context).isFinishing()) {
                        ((Activity) context).onBackPressed();
                    }
                }
            }
        });
        scaleImageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (loadPhotoListener != null) {
                    loadPhotoListener.longClick(position);
                }
                return false;
            }
        });

        container.addView(itemView); //// TODO: 2017/5/17
        return itemView;
//        return
/*

//        GlideUitl.showImageView(picAddress.get(position),smallPhoto);
        Glide.with(RuiApplication.getAppContext()).load(picAddress.get(position))
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .crossFade()
                .thumbnail(0.5f)
                .centerCrop()
                .listener(new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
//                        smallPhoto.setVisibility(View.GONE);

//                        resource.
                        if (loadPhotoListener != null) {
                            loadPhotoListener.loadingFinish();
                        }
                        return false;
                    }
                })
                .into(imageView);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (context instanceof Activity) {
                    if (!((Activity) context).isFinishing()) {
                        ((Activity) context).onBackPressed();
                    }
                }
            }
        });
        imageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (loadPhotoListener != null) {
                    loadPhotoListener.longClick(position);
                }
                return false;
            }
        });
        container.addView(itemView); //// TODO: 2017/5/17
        return itemView;
        */

    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getCount() {
        if (picAddress != null) {
            return picAddress.size();
        }
        return 0;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    public void setPicAddress(ArrayList<String> picAddress) {
        this.picAddress = picAddress;
        notifyDataSetChanged();
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    public void setLoadPhotoListener(LoadPhotoListener loadPhotoListener) {
        this.loadPhotoListener = loadPhotoListener;
    }

    @Override
    public void onPhotoTap(View view, float x, float y) {
        if (loadPhotoListener != null) {
            loadPhotoListener.onTapClick();
        }
    }

    public interface LoadPhotoListener {
        void startLoading();

        void loadingFinish();

        void longClick(int position);

        void onTapClick();
    }
}
